<template>
    <!-- 项目选择 -->
    <a-select :dropdownClassName="isCustomClass ? 'ant-select-dropdown-top30' : ''" :allowClear="allowClear" showSearch
        :getPopupContainer="e => e.parentNode" :filter-option="filterOption" v-bind="$attrs" v-on="$listeners"
        :placeholder="placeholder" v-model="currentValue">
        <a-icon v-if="loading" style="color: #096dd9" slot="suffixIcon" type="loading" />
        <a-spin v-if="loading" slot="notFoundContent" size="small" />
        <a-select-option v-for="i in project" :key="i.projectId" :value="i.projectId">{{ i.projectName }}
        </a-select-option>
    </a-select>
</template>
<script>
import { getAuditProject } from '@/api/themeChoose/themeChoose'

/*
* 根据对应的权限查项目
*
* */
export default {
    name: 'SelectProject',
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String, Object, Number]
        },
        isCustomClass: {
            type: Boolean,
            default: () => false
        },

        // placeholder
        placeholder: {
            type: String,
            default: () => '请选择'
        },
        // 是否自动选中第一行
        autoSelected: {
            type: Boolean,
            default: () => true
        },
        // 是否自动选中第一行
        allowClear: {
            type: Boolean,
            default: () => true
        }
    },
    mounted() {
        this.getList()
    },
    data() {
        return {
            loading: false, // 加载控件
            project: []// 项目列表
        }
    },
    methods: {
        /**
         * 获取当前所选项目的项目名
         */
        getLabel() {
            return this.project.filter(item => item.projectId === this.value)[0]?.projectName
        },
        /**
         * 获取项目列表
         */
        async getList() {
            this.loading = true
            let res = await getAuditProject({ auditStatus: 2 })
            // let res = await getAuditProject({ auditStatus: this.auditStatus })
            this.project = res.data
            if (this.autoSelected && (!this.value)) {
                this.currentValue = this.project && this.project.length ? this.project[0].projectId : ''
            }
            this.loading = false
        },
        filterOption(input, option) {
            return (option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0)
        }
    },
    computed: {
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function () {
                return this.value
            },
            set: function (value) {
                this.$emit('input', value)
                let res = this.project.find(v => v.projectId === value)
                //和原生事件冲突
                res && this.$emit('selectChange', res)
            }
        }
    }
}
</script>
